/*
Copyright (c) 2010 k.schulz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package de.altitudecustomcommands;

import de.altitude.cimuelle.Config;
import de.altitude.cimuelle.MessageTask;
import de.altitudecustomcommands.exceptions.ServerNotFoundException;
import org.w3c.dom.NodeList;

import javax.naming.ConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * This class contains the informations of all of your virtual servers. It also includes methods to search for a
 * specific server.
 *
 * @author k.schulz
 */
public class serverInformations {

    private virtualServer[] servers = null;

    /**
     * This method returns the amount of virtualServers you are running.
     *
     * @return Returns the amount of virtual server you are running.
     */


    public int getServerNumber() {
        return this.servers.length;
    }

    /**
     * This method returns a single virtualServer object out of your virtualServer list.
     *
     * @param index A servernumber. This number is the internal server representation and not the port.
     *              This method can be used together with <a href="serverInformations.html#getServerNumber()"getServer()> getServerNumber()</a>
     *              to get all servers.
     * @return Returns a virtualServer object.
     */

    public virtualServer getServer(int index) {
        return this.servers[index];
    }

    /**
     * This method searches your list of virtual servers for a server with a specific port.
     *
     * @param port The port of the virtual server you are searching.
     * @return Returns the virtualServer object, with the port given as argument.
     * @throws ServerNotFoundException This exception is thrown if there is no virtual server with the port, given as argument.
     */

    public virtualServer getServerByPort(int port) throws ServerNotFoundException {
        for (virtualServer server : this.servers) {
            if (port == server.getPort()) return server;
        }
        throw new ServerNotFoundException();
    }

    public virtualServer getServerByPort(String sport) throws ServerNotFoundException {
        int port = Integer.parseInt(sport);
        for (virtualServer server : this.servers) {
            if (port == server.getPort()) return server;
        }
        throw new ServerNotFoundException();
    }

    /**
     * This method searches your list of virtual servers for a server with a specific name.
     *
     * @param name The name of the virtual server you are searching.
     * @return Returns the virtualServer object, with the name given as argument.
     * @throws ServerNotFoundException This exception is thrown if there is no virtual server with the name, given as argument.
     */

    public virtualServer getServerByName(String name) throws ServerNotFoundException {
        for (virtualServer server : this.servers) {
            if (name.equals(server.getName())) return server;
        }
        throw new ServerNotFoundException();
    }

    /**
     * Constructor which constructs a new serverInformation object. It will parse
     * the the launcher_config.xml in your altitude/server directory and creates
     * new virtualServer objects with every information needed.
     *
     * @param path The path to your altitude/server directory.
     * @throws javax.xml.parsers.ParserConfigurationException
     *
     * @throws org.xml.sax.SAXException
     * @throws java.io.IOException
     */

    public serverInformations(String path) throws
            javax.xml.parsers.ParserConfigurationException,
            org.xml.sax.SAXException,
            java.io.IOException {
        NodeList list = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder()
                .parse(path + "launcher_config.xml")
                .getElementsByTagName("AltitudeServerConfig");
        this.servers = new virtualServer[list.getLength()];
        for (int i = 0; i < list.getLength(); i++) {
            this.servers[i] = new virtualServer(list.item(i));
        }
    }

    public serverInformations() throws
            javax.xml.parsers.ParserConfigurationException,
            org.xml.sax.SAXException,
            java.io.IOException, ConfigurationException {
        NodeList list = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder()
                .parse(Config.getInstance().getLauncherConfig())
                .getElementsByTagName("AltitudeServerConfig");
        this.servers = new virtualServer[list.getLength()];
        for (int i = 0; i < list.getLength(); i++) {
            this.servers[i] = new virtualServer(list.item(i));
            checkTimerMessage(String.valueOf(servers[i].getPort()));
        }

    }

    void checkTimerMessage(String port) throws ConfigurationException {
        Config config = Config.getInstance();
        String timer = config.getConfig(port + ".timerMessageIntervall", "");
        if (!"".equals(timer)) {
            MessageTask mt = MessageTask.getInstance();
            mt.startMessageTask(port);
        }
    }

}
